package com.etc.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.entity.Student;
import com.etc.service.StudentService;
import com.etc.service.impl.StudentServiceImpl;
import com.etc.util.PageData;
import com.google.gson.Gson;

/**
 * Servlet implementation class StudentServlet
 */
@WebServlet("/StudentServlet")
public class StudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private StudentService ss = new StudentServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StudentServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		String op = "default";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if ("default".equals(op)) {
			doDefault(request, response);
		} else if ("del".equals(op)) {
			doDel(request, response);
		} else if ("update".equals(op)) {
			doUpdate(request, response);
		} else if ("add".equals(op)) {
			doAdd(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	/**
	 * 添加
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void doAdd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

	/**
	 * 编辑
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void doUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		String stuname = "";
		int stuage = 0;
		int stuid = 0;
		String msg = "";
		String stusex = "";
		int status = -1;
		if (request.getParameter("stuname") != null) {
			stuname = request.getParameter("stuname");
		}
		if (request.getParameter("stuage") != null) {
			stuage = Integer.parseInt(request.getParameter("stuage"));
		}
		if (request.getParameter("stuid") != null) {
			stuid = Integer.parseInt(request.getParameter("stuid"));
		}
		if (request.getParameter("stusex") != null) {
			stusex = request.getParameter("stusex");
		}
		if (request.getParameter("status") != null) {
			status = Integer.parseInt(request.getParameter("status"));
		}
		if (!"".equals(stuname) && stuage > 0 && stuid > 0) {
			Student st = new Student(stuid, stuname, stuage, stusex, status);
			boolean flag = ss.updateStudent(st);
			if (flag) {
				msg = "更新成功";
				out.print(gson.toJson(msg));
			} else {
				msg = "更新失败";
				System.out.println("*********");
				out.print(gson.toJson(msg));
			}
		} else {
			msg = "更新失败";
			out.print(gson.toJson(msg));
		}
	}

	/**
	 * 删除
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void doDel(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		Gson gson = new Gson();
		int stuid = 0;
		String msg = "";
		if (request.getParameter("stuid") != null) {
			stuid = Integer.parseInt(request.getParameter("stuid"));
		}
		if (stuid > 0) {
			boolean flag = ss.delStudent(stuid);
			if (flag) {
				msg = "删除成功";

				out.print(gson.toJson(msg));
			} else {
				msg = "删除失败";
				out.print(gson.toJson(msg));
			}
		} else {
			msg = "删除失败";
			out.print(gson.toJson(msg));
		}
	}

	/**
	 * 表格初始化
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void doDefault(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		int page = 1;
		int limit = 10;
		String content = "";
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("limit") != null) {
			limit = Integer.parseInt(request.getParameter("limit"));
		}
		if (request.getParameter("content") != null) {
			content = request.getParameter("content");
		}
		PageData<Student> pd = ss.getStudent(page, limit, content);
		pd.setCode(0);
		pd.setMsg("");
		Gson gson = new Gson();
		String str = gson.toJson(pd);
		out.print(str);
		out.close();
	}
}
